package com.e9ab98e991ab.moduler.ui.pay

import android.text.TextUtils
import android.view.View

import com.e9ab98e991ab.libcommon.base.BaseActivity
import com.e9ab98e991ab.libcommon.utils.toast.ToastyUtils
import com.e9ab98e991ab.moduler.R
import com.e9ab98e991ab.moduler.library.pay.alipay.Alipay
import kotlinx.android.synthetic.main.activity_ali.*


class AliActivity : BaseActivity() {

    override fun getLayoutId(): Int {
        return R.layout.activity_ali
    }



    override fun initSet() {
        setTitle(R.string.alipay)
        aliConfirm.setOnClickListener { _: View? ->
            run {
                if (!TextUtils.isEmpty(aliParam.text.toString())) {
                    doAlipay(aliParam.text.toString())
                }
            }
        }
    }

    override fun initDate() {
    }


    /**
     * 支付宝支付
     *
     * @param payParam 支付服务生成的支付参数
     */
    private fun doAlipay(payParam: String) {
        Alipay(this, payParam, object : Alipay.AlipayResultCallBack {
            override fun onSuccess() {
                ToastyUtils.success(R.string.pay_processing)
            }

            override fun onDealing() {
                ToastyUtils.normal(R.string.pay_processing)
            }

            override fun onError(errorCode: Int) {
                when (errorCode) {

                    Alipay.ERROR_RESULT -> ToastyUtils.error(R.string.pay_result_parsing_error)

                    Alipay.ERROR_NETWORK ->  ToastyUtils.error(R.string.pay_network_connection_error)

                    Alipay.ERROR_PAY -> ToastyUtils.error(R.string.pay_payment_code_error)

                    else -> ToastyUtils.error(R.string.pay_error)
                }
            }

            override fun onCancel() {
                ToastyUtils.normal(R.string.pay_cancel)

            }
        }).doPay()
    }


}